layui.define(['layer', 'form', 'jquery', 'laytpl'], function (exports) {
  var $ = layui.$,
      layer = layui.layer,
      laytpl = layui.laytpl;

  var pageConf = {
    page: 0,
    size: 10
  }

  var userConf = {
    key_id: '',
    user_id: '0',
    secret: ''
  }
  var sales = {
    init: function () {
      $('.sell').addClass('on');
    },
    bindEvent: function () {
      //tab 切换事件
      $('.nav ul li').click(function () {
        $(this).addClass('on').siblings().removeClass('on');
        $('.tab_container .content').eq(
            $(this).index()).show().siblings().hide();
        var tab_index = $(this).index();
        if(tab_index==0){
          sales.initSalesPagination();
        }else if(tab_index==1){
          sales.initSettlementPagination();
        }
      })

      //提交事件
      $(":submit").click(function () {
        if($('.con8-r .nav ul li:first').hasClass('on')){
          sales.initSalesPagination();
        }else{
          sales.initSettlementPagination();
        }
      })
    },
    initSalesPagination: function () {
      $(".pages").pagination({
        showData: 10,
        coping: true,
        prevContent: '上一页',
        nextContent: '下一页',
        pageCount: sales.loadSalesData(),
        callback: function (api) {
          pageConf.page = api.getCurrent();
          sales.loadSalesData();
        }
      })
    },
    loadSalesData: function () {
      var data = $('.search_form').serializeJSON(),
          totalPages = 0;
      var index = layer.load(1, {shade: [0.1, '#fff']});
      $.ajax({
        url: '/getUserOrderList',
        data: $.extend(pageConf, data, userConf),
        type: 'post',
        dataType: 'json',
        async: false,
        success: function (result) {
          layer.close(index);
          var total = result.total;
          if (total == ""||total==0) {
            $('.sales_list').hide();
            $('.nothing').show();
          } else {
            $('.sales_list').show();
            $('.nothing').hide();
            var getTpl = $('#sales_body').html(),
                tbody = $("#sales_table tbody");
            laytpl(getTpl).render(result, function (html) {
              tbody.html(html);
            });
            totalPages = Math.ceil(total / 10);
          }

        }
      })
      return totalPages;
    },
    initSettlementPagination: function () {
      $(".pages").pagination({
        showData: pageConf.size,
        coping: true,
        prevContent: '上一页',
        nextContent: '下一页',
        pageCount:sales.loadSettlementData(),
        callback: function (api) {
          pageConf.page = api.getCurrent();
          sales.loadSettlementData();
        }
      })
    },
    loadSettlementData: function () {
      var data = $('.search_form').serializeJSON(),
          totalPages = 0;
      data.search_key = data.worksNameOrDciCode;
      var index = layer.load(1, {shade: [0.1, '#fff']});
      $.ajax({
        url: '/getUserSettlementList',
        data: $.extend(pageConf, data, userConf),
        type: 'post',
        dataType: 'json',
        async: false,
        success: function (result) {
          layer.close(index);
          var total = result.total;
          if (total == ""||total==0) {
            $('.settle_lists').hide();
            $('.nothing').show();
          } else {
            $('.settle_lists').show();
            $('.nothing').hide();
            var getTpl = $('#settle_body').html(),
                tbody = $("#settle_table tbody");
            laytpl(getTpl).render(result, function (html) {
              tbody.html(html);
            });
            totalPages = Math.ceil(total / 10);
          }
        }
      })
      return totalPages;
    },
    loadSecret: function () {
      $.ajaxSettings.async = false;
      $.getJSON('/copyright/getUserIdAndSecret', function (result) {
        userConf.key_id = result.keyId;
        userConf.user_id = result.userId;
        userConf.secret = result.secret;
      })
    }
  }
  sales.init();
  sales.bindEvent();
  sales.loadSecret();
  sales.initSalesPagination();
  exports('sales', {});
});